﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADO.NET.Samples.Common;

namespace ADO.NET.Samples.Forms
{
    public partial class MainForm : Form
    {
        private FormsDataBindingList bindingList;

        public MainForm()
        {
            InitializeComponent();
            bindingList = new FormsDataBindingList();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// Bind data
        /// </summary>
        private void BindData()
        {
            this.listBoxForms.DataSource = this.bindingList;
            this.listBoxForms.DisplayMember = "FormTitle";
            this.listBoxForms.ValueMember = "FormType";
        }

        /// <summary>
        /// Show content
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBoxForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            var info = this.listBoxForms.SelectedItem as FormInfo;
            if (info != null)
            {
                this.textBoxFormTitle.Text = info.FormTitle;
                this.textBoxFormDesc.Text = info.FormDesc;
                this.textBoxFormType.Text = info.FormType;
            }
        }

        /// <summary>
        /// load form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLoadForm_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBoxFormType.Text))
            {
                try
                {
                    Form form = Activator.CreateInstance(Type.GetType(this.textBoxFormType.Text)) as Form;
                    form.Text = this.textBoxFormTitle.Text;
                    form.Show();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}
